package com.guapi.controller;

import com.guapi.entity.Discuss;
import com.guapi.entity.User;
import com.guapi.service.DiscussService;
import com.guapi.service.ReplyService;
import com.guapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
public class DiscussController {
    @Autowired
    private ReplyService replyService;

    @Autowired
    private DiscussService discussService;

    @Autowired
    private UserService userService;

    private List<Discuss> discussList;

    private List<User> userList ;


    public void init(){
        discussList=discussService.getCache("discussList");
        userList=userService.getCache("userList");
    }
    public void addCache(){
        discussService.setCache("discussList",discussList);
        userService.setCache("userList",userList);
    }
    public void removeCache(){
        discussService.deleteCache("discussList");
        userService.deleteCache("userList");
    }

    @GetMapping("/discuss")
    public String getAllDiscuss(Model model){
        init();
        if(discussList == null || discussList.size() == 0){

            discussList = discussService.getAllDiscuss();
            discussService.setCache("discussList",discussList);
            discussList = discussService.getCache("discussList");

            model.addAttribute("userList",userList);
            model.addAttribute("discussList",discussList);
            model.addAttribute("sum",discussList.size()==0?0:discussList.size());
            return "page/discuss";
        }

        model.addAttribute("userList",userList);
        model.addAttribute("discussList",discussList);
        model.addAttribute("sum",discussList.size()==0?0:discussList.size());
        return "page/discuss";
    }


    @PostMapping("/discuss/insert")
    public String insert(
            @RequestParam("title") String title,
            @RequestParam("content") String content,
            @RequestParam("username") String username){
        //        首先清除缓存
        removeCache();
        Discuss discuss = new Discuss();
        discuss.setTitle(title);
        discuss.setContent(content);
        discuss.setUsername(username);
        discuss.setDate(new Date());
        discussService.insert(discuss);
        discussList = discussService.getAllDiscuss();

        addCache();
        return "page/discuss";
    }

    @DeleteMapping("/discuss/delete/{id}")
    public String delete(@PathVariable("id") String id){
        removeCache();

        discussService.delById(id);
        discussList= discussService.getAllDiscuss();

        addCache();
        return "page/discuss";
    }

    @PostMapping("/discuss/update")
    public String update(
            @RequestParam("title") String title,
            @RequestParam("content") String content,
            @RequestParam("username") String username){

        removeCache();

        Discuss discuss = new Discuss();
        discuss.setTitle(title);
        discuss.setContent(content);
        discuss.setUsername(username);
        discuss.setDate(new Date());
        discussService.insert(discuss);
        discussList = discussService.getAllDiscuss();
        //重新设置缓存
        addCache();
        return "page/discuss";
    }
}
